package com.lianboke.link.ad.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lianboke.link.ad.R;

import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.updateUtils.AppUtils;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.KLog;
import com.lianboke.link.ad.utils.NetWorkUtils;
import com.lianboke.link.ad.utils.SystemUtils;
import com.lianboke.link.ad.utils.ToastUtil;

import java.lang.ref.WeakReference;

import abc.abc.abc.AdManager;
import butterknife.BindView;
import cn.waps.AppConnect;

/**
 * 启动
 */
public class SplashActivity extends BaseActivity {

    @BindView(R.id.view_head)
    public View view_head;

    @BindView(R.id.tv_splash)
    public ImageView tv_splash;
    @BindView(R.id.tv_splash_name)
    public TextView tv_splash_name;

    @BindView(R.id.tv_splash_netstatus)
    public TextView tv_splash_netstatus;

    @BindView(R.id.login_progress)
    public ProgressBar login_progress;

    private MySplashHandler mySplashHandler;

    @Override
    public int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initActivityParams(Bundle savedInstanceState) {
        //发布版本需要更改为
        //有米
        AdManager.getInstance(actContext).init(CommonParams.YOUMI_APP_ID, CommonParams.YOUMI_APP_SECRET, CommonParams.isEnableYoumiLog);
       //万普
        AppConnect.getInstance(CommonParams.WANPU_APP_ID,CommonParams.WANPU_APP_PID,this);
        view_head.setVisibility(View.GONE);
        mySplashHandler = new MySplashHandler(this);
        tv_splash_netstatus.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //判断手机系统的版本  即API大于10 就是3.0或以上版本
                Intent intent = null;
//                if (android.os.Build.VERSION.SDK_INT > 13) {
//                    // 3.2以上打开设置界面，也可以直接用ACTION_WIRELESS_SETTINGS打开到wifi界面
//                    intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
//                } else {
//                    intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
//                }
                intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS);
                actContext.startActivity(intent);
            }
        });
        if(SystemUtils.isRootSystem()) {
            //判断是否已root
            showLoginProgress();
            //检测网络状态
            checkNetWorkStatusParams();
        }else{
            //没有root 权限
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.notify_root_title);
            builder.setMessage(R.string.notify_root_content);
            // 拒绝, 退出应用
            builder.setNegativeButton(R.string.ok,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if(AppUtils.hasApp(actContext,"com.kingroot.kinguser")){
                                DeviceUtils.doStartApplicationWithPackageName(actContext,"com.kingroot.kinguser");
                            }
                            finish();
                        }
                    });
            builder.setCancelable(false);
            builder.show();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideLoginProgress();
        if(mySplashHandler!=null){
            mySplashHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public boolean initLocalBroadcastReceiver() {
        return true;
    }
    public void showLoginProgress(){
        if(login_progress!=null && login_progress.getVisibility()!=View.VISIBLE){
            login_progress.setVisibility(View.VISIBLE);
        }
    }
    public void hideLoginProgress(){
        if(login_progress!=null && login_progress.getVisibility()!=View.GONE){
            login_progress.setVisibility(View.GONE);
        }
    }
    /**
     * 检测网络状态
     */
    public void checkNetWorkStatusParams(){
        if(NetWorkUtils.isNetworkConnected(actContext) && "WIFI".equals(DeviceUtils.getDeviceAc())){
            ToastUtil.showShort(actContext,"关闭wifi连接，打开移动网络!");
        }
        if( NetWorkUtils.isNetworkConnected(actContext) && DeviceUtils.isMobileNetTypeByCustomer(actContext)){
            //网络连接正常
            mySplashHandler.sendEmptyMessageDelayed(iHanderType_GOTO_LONGIN_TYPE,1000*3);
        }else{
            hideLoginProgress();
            tv_splash.setVisibility(View.GONE);
            tv_splash_name.setVisibility(View.GONE);
            tv_splash_netstatus.setVisibility(View.VISIBLE);
        }
    }



    /**
     * 跳转任务主页面
     */
    public void gotomLoginActiviy(){
        Intent mIntent = new Intent();
        mIntent.setClass(SplashActivity.this,LoginActivity.class);
        startActivity(mIntent);
        finish();
    }
    public static final int iHanderType_GOTO_LONGIN_TYPE = 2020;//跳转到登录页面
    /**
     * 主页handler
     */
    public static class MySplashHandler extends Handler {
        WeakReference<BaseActivity> softReference;

        public MySplashHandler(BaseActivity activity) {
            softReference = new WeakReference<BaseActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SplashActivity mActivity = (SplashActivity) softReference.get();
            if (mActivity != null) {
                int iWhateType = msg.what;
                switch (iWhateType) {
                    case iHanderType_GOTO_LONGIN_TYPE:
                        mActivity.hideLoginProgress();
                        mActivity.gotomLoginActiviy();
                        break;
                }
            }
        }
    }

    //=============================接受器=================================


    @Override
    public void getMyBroadcastReceiver(String netType) {
        super.getMyBroadcastReceiver(netType);
        if(mySplashHandler!=null){
            mySplashHandler.removeCallbacksAndMessages(null);
        }
        if(NetWorkUtils.isNetworkConnected(actContext) && DeviceUtils.isMobileNetTypeByCustomer(actContext)){
            //网络连接正常
            tv_splash.setVisibility(View.VISIBLE);
            tv_splash_name.setVisibility(View.VISIBLE);
            tv_splash_netstatus.setVisibility(View.GONE);
            mySplashHandler.sendEmptyMessageDelayed(iHanderType_GOTO_LONGIN_TYPE,1000*3);
        }else{
            hideLoginProgress();
            tv_splash.setVisibility(View.GONE);
            tv_splash_name.setVisibility(View.GONE);
            tv_splash_netstatus.setVisibility(View.VISIBLE);
        }
    }
}
